/*
    Project:        Home Power Control
    File:           HPCClient.h
    Author:         christian@karunamusic.at
    Date:           01.2015
    Description:    The HPC Client.
 */

#ifndef HPCCLIENT_H
#define HPCCLIENT_H

#include <QMap>
#include <QStringList>
#include <QDomDocument>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <HPCSensor.h>
#include "HPCApplication.h"

class HPCClient : public HPCApplication
{
    Q_OBJECT
public:
    /** \brief Constructor with arguments from cpmmandline (main).
     */
    explicit HPCClient( int& argc, char** argv );
    /** \brief Destructor.
     */
    virtual ~HPCClient();

    /** \brief Get name of the client (as configured in the .ini file).
     *
     * \return string with name.
     */
    inline QString Name() const
        { return m_Name; }

signals:

public slots:
    /** \brief Run the eventloop.
     */
    void Run();

    /** \brief Receive command from cli.
     *
     * \param cmd the command.
     */
    void command( QString cmd );

    /** \brief Slot for the change-signal of the sensor-value.
     *
     * \param sender Sensor which have sent the signal.
     * \param val changed value of the sensor.
     */
    void ValueChanged( HPCSensor* sender, HPCSensorValue& val );
    /** \brief Slot for the alarm-signal of the sensor-value.
     *
     * \param sender Sensor which have sent the signal.
     * \param val alarmed value of the sensor.
     * \param gt if true alarm is raised because of val >= max, otherwise val <= min.
     * \param ndx index of the fired alarm-value in the Min/Max-vector of HPCSensorValue.
     */
    void ValueAlarmed( HPCSensor* sender, HPCSensorValue& val, bool gt, unsigned int ndx );

    /** \brief client is connected to the server.
     */
    void connected();
    /** \brief client has disconnected from the server.
     */
    void disconnected();
    /** \brief Number of byte sent to server.
     */
    void bytesWritten(qint64 bytes);
    /** \brief New request from tha server.
     */
    void readyRead();

protected:
    /** \brief Read configuration from .ini file.
     */
    virtual void readConfig();
    /** \brief Called if help from cli is requested.
     *
     * \param topic Helptopic or empty string for general help.
     */
    virtual void cliHelp( QString topic = "" );

private:
    /** \brief Create sensors from configuration.
     *
     * \param doc xml document with configuration.
     */
    QString m_Name;
    QString m_hpcServer;
    unsigned int m_hpcPort;
    QTcpSocket* m_Socket;
    bool m_Connected;
    QMap<QString,HPCSensor*> m_Sensors;
};

QDataStream& operator>> ( QDataStream& is, QMap<QString,HPCSensor*>& smap );

#endif // HPCCLIENT_H
